<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>系统设置管理</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
    /* 基础重置 */
    * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
        font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    }

    /* 增强版主容器 */
    .module-section {
        padding: 2rem 4%;
        background: #f8fafc;
        min-height: 100vh;
    }

    /* 卡片样式升级 */
    .card {
        background: white;
        border-radius: 16px;
        box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
        padding: 2rem;
        margin-bottom: 2rem;
        border: 1px solid #e2e8f0;
        transition: transform 0.2s;
    }

    .card:hover {
        transform: translateY(-2px);
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.1);
    }

    /* 标题增强 */
    .module-title {
        font-size: 1.75rem;
        color: #1e293b;
        margin-bottom: 2rem;
        display: flex;
        align-items: center;
        gap: 1rem;
        padding-bottom: 1rem;
        border-bottom: 2px solid #f1f5f9;
    }

    .module-title i {
        font-size: 1.5em;
        color: #7c3aed;
    }

    /* 网格布局优化 */
    .settings-grid {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(380px, 1fr));
        gap: 2rem;
        margin-top: 1.5rem;
    }

    /* 表单样式升级 */
    .multi-column-form {
        display: grid;
        grid-template-columns: repeat(2, 1fr);
        gap: 1.5rem;
    }

    .form-group {
        margin-bottom: 1.5rem;
    }

    .form-group label {
        display: block;
        margin-bottom: 0.75rem;
        color: #475569;
        font-weight: 600;
        font-size: 1rem;
    }

    input, select, textarea {
        width: 100%;
        padding: 1rem;
        border: 2px solid #e2e8f0;
        border-radius: 12px;
        font-size: 1rem;
        transition: all 0.2s;
    }

    input:focus, select:focus, textarea:focus {
        border-color: #7c3aed;
        box-shadow: 0 0 0 4px rgba(124, 58, 237, 0.1);
    }

    /* 列表项样式优化 */
    .list-item {
        padding: 1.5rem;
        margin: 1rem 0;
        background: #f8fafc;
        border-radius: 12px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        transition: background 0.2s;
    }

    .list-item:hover {
        background: #f1f5f9;
    }

    .list-item h3 {
        font-size: 1.1rem;
        color: #1e293b;
        margin-bottom: 0.25rem;
    }

    .list-item small {
        font-size: 0.9rem;
        color: #64748b;
    }

    /* 开关控件升级 */
    .switch-container {
        display: flex;
        align-items: center;
        gap: 1.5rem;
    }

    .switch {
        position: relative;
        display: inline-block;
        width: 56px;
        height: 32px;
    }

    .slider {
        height: 32px;
        background: #e2e8f0;
        border-radius: 16px;
    }

    .slider:before {
        width: 28px;
        height: 28px;
        left: 2px;
        bottom: 2px;
    }

    input:checked + .slider {
        background: #7c3aed;
    }

    input:checked + .slider:before {
        transform: translateX(24px);
    }

    /* 上传区域优化 */
    .upload-box {
        border: 2px dashed #e2e8f0;
        border-radius: 16px;
        padding: 2rem;
        margin-top: 1.5rem;
        text-align: center;
        cursor: pointer;
        transition: all 0.2s;
    }

    .upload-box:hover {
        border-color: #7c3aed;
        background: rgba(124, 58, 237, 0.05);
    }

    .upload-box i {
        font-size: 2rem;
        color: #7c3aed;
        margin-bottom: 1rem;
    }

    /* 按钮样式增强 */
    .submit-btn {
        padding: 1rem 2rem;
        font-size: 1rem;
        border-radius: 12px;
        background: #7c3aed;
        color: white;
        display: inline-flex;
        align-items: center;
        gap: 0.75rem;
    }

    .submit-btn:hover {
        background: #6d28d9;
        transform: translateY(-1px);
    }

    /* 响应式优化 */
    @media (max-width: 1024px) {
        .settings-grid {
            grid-template-columns: 1fr;
        }
        
        .multi-column-form {
            grid-template-columns: 1fr;
        }
        
        .module-section {
            padding: 1.5rem;
        }
    }

    @media (max-width: 480px) {
        .card {
            padding: 1.5rem;
        }
        
        .list-item {
            flex-direction: column;
            align-items: stretch;
            gap: 1rem;
        }
    }

    /* 新增视觉元素 */
    .section-divider {
        margin: 2rem 0;
        border: 0;
        height: 1px;
        background: #e2e8f0;
    }

    .config-badge {
        padding: 0.5rem 1rem;
        border-radius: 8px;
        background: #e0f2fe;
        color: #0ea5e9;
        font-size: 0.9rem;
    }
    </style>
</head>
<body>
    <div class="module-section">
        <!-- 田字格设置区块 -->
        <div class="settings-grid">
            <!-- 门店基础设置 -->
            <div class="card">
                <h2 class="module-title">
                    <i class="fas fa-store"></i>
                    门店基础设置
                </h2>
                <form class="multi-column-form" id="store-base-form">
                    <div class="form-group">
                        <label>门店名称</label>
                        <input type="text" id="store-name" value="沃尔玛朝阳分店" required>
                    </div>
                    <div class="form-group">
                        <label>营业时间</label>
                        <input type="text" id="store-time" value="08:00 - 22:00" required>
                    </div>
                    <div class="form-group">
                        <label>服务热线</label>
                        <input type="tel" id="store-phone" value="010-87654321">
                    </div>
                    <div class="form-group">
                        <label>门店地址</label>
                        <textarea id="store-addr" rows="2">北京市朝阳区建国路88号</textarea>
                    </div>
                    <hr class="section-divider">
                    <div class="form-group">
                        <button type="submit" class="submit-btn">
                            <i class="fas fa-save"></i>
                            保存设置
                        </button>
                    </div>
                </form>
            </div>
            <!-- 扣款规则管理 -->
            <div class="card">
                <h2 class="module-title">
                    <i class="fas fa-money-bill-wave"></i>
                    扣款规则管理
                </h2>
                <form class="multi-column-form">
                    <div class="form-group">
                        <label>迟到扣款金额（元）</label>
                        <input type="number" min="0" step="1" value="50" id="lateDeduct">
                    </div>
                    <div class="form-group">
                        <label>早退扣款金额（元）</label>
                        <input type="number" min="0" step="1" value="50" id="leaveEarlyDeduct">
                    </div>
                    <div class="form-group">
                        <label>缺勤扣款金额（元/天）</label>
                        <input type="number" min="0" step="1" value="100" id="absentDeduct">
                    </div>
                    <div class="form-group" style="grid-column: span 2;">
                        <button type="button" class="submit-btn">
                            <i class="fas fa-save"></i>
                            保存规则
                        </button>
                    </div>
                </form>
            </div>
            <!-- 会员规则设置 -->
            <div class="card">
                <h2 class="module-title">
                    <i class="fas fa-users"></i>
                    会员规则设置
                </h2>
                <form class="multi-column-form" id="point-rule-form">
                    <div class="form-group" style="grid-column: span 2;">
                        <label>积分累计规则</label>
                        <div style="display:flex;align-items:center;gap:1rem;">
                            <span>当前比例：1元=</span>
                            <input type="number" id="pointRate" value="1" min="0" step="0.1" style="width:80px;"> <span>积分</span>
                        </div>
                    </div>
                    <div class="form-group" style="grid-column: span 2;">
                        <button type="button" class="submit-btn" id="savePointRuleBtn">
                            <i class="fas fa-save"></i>
                            保存规则
                        </button>
                    </div>
                </form>
            </div>
            <!-- 系统维护管理 -->
            <div class="card">
                <h2 class="module-title">
                    <i class="fas fa-shield-alt"></i>
                    系统维护管理
                </h2>
                <div class="list-item">
                    <div>
                        <h3>数据备份</h3>
                        <small>最后备份：<span id="backupTime">-</span></small>
                    </div>
                    <button type="button" class="submit-btn" id="exportBackupBtn">
                        <i class="fas fa-database"></i>
                        导出数据
                    </button>
                </div>
                <div class="list-item">
                    <div>
                        <h3>操作日志</h3>
                        <small>最后保存：<span id="logTime">-</span></small>
                    </div>
                    <button type="button" class="submit-btn" id="exportLogBtn">
                        <i class="fas fa-file-csv"></i>
                        导出日志
                    </button>
                </div>
                <div class="list-item">
                    <div>
                        <h3>系统版本</h3>
                        <small>当前版本：v2.3.1</small>
                    </div>
                    <span class="config-badge">
                        <i class="fas fa-check-circle"></i>
                        最新版本
                    </span>
                </div>
            </div>
        </div>
    </div>
    <script>
    function loadDeductRule() {
        fetch('/api/deduct-rule')
            .then(res => res.json())
            .then(resp => {
                if (resp.success && resp.data) {
                    document.getElementById('lateDeduct').value = resp.data.late;
                    document.getElementById('leaveEarlyDeduct').value = resp.data.early;
                    if(resp.data.absent !== undefined) document.getElementById('absentDeduct').value = resp.data.absent;
                }
            });
    }
    loadDeductRule();

    function saveDeductRule() {
        var late = document.getElementById('lateDeduct') && document.getElementById('lateDeduct').value;
        var early = document.getElementById('leaveEarlyDeduct') && document.getElementById('leaveEarlyDeduct').value;
        var absent = document.getElementById('absentDeduct') && document.getElementById('absentDeduct').value;
        if(late === undefined || early === undefined || absent === undefined) {
            alert('未找到输入框');
            return;
        }
        fetch('/api/deduct-rule', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ late: late, early: early, absent: absent })
        })
        .then(res => res.json())
        .then(resp => {
            if (resp.success) {
                alert('保存规则成功');
                loadDeductRule();
            } else {
                alert(resp.message || '保存失败');
            }
        })
        .catch(()=>alert('网络错误'));
    }
    // 自动绑定保存规则按钮事件
    (function(){
        var btns = document.querySelectorAll('.card .submit-btn');
        btns.forEach(function(btn) {
            if (btn.textContent.includes('保存规则')) {
                btn.addEventListener('click', saveDeductRule);
            }
        });
    })();

    // 页面加载时自动获取积分规则
    function loadPointRule() {
        fetch('/api/point-rule')
            .then(res => res.json())
            .then(resp => {
                if (resp.success && resp.data && resp.data.rate !== undefined) {
                    document.getElementById('pointRate').value = resp.data.rate;
                }
            });
    }
    loadPointRule();

    // 积分累计规则保存
    function savePointRule() {
        var rate = document.getElementById('pointRate').value;
        fetch('/api/point-rule', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ rate: rate })
        })
        .then(res => res.json())
        .then(resp => {
            if (resp.success) {
                alert('积分累计规则修改成功');
                loadPointRule();
            } else {
                alert(resp.message || '保存失败');
            }
        })
        .catch(()=>alert('网络错误'));
    }
    document.getElementById('savePointRuleBtn').onclick = savePointRule;

    // 添加系统维护导出功能
    // 加载备份信息
    function loadBackupInfo() {
        fetch('/api/backup/info')
            .then(res => res.json())
            .then(resp => {
                if (resp.success) {
                    document.getElementById('backupTime').textContent = resp.last_backup || '-';
                }
            });
    }
    loadBackupInfo();

    // 导出数据库按钮事件
    document.getElementById('exportBackupBtn').onclick = function() {
        window.location.href = '/api/backup/export';
        setTimeout(loadBackupInfo, 1000);
    };

    // 加载日志信息
    function loadLogsInfo() {
        fetch('/api/logs/info')
            .then(res => res.json())
            .then(resp => {
                if (resp.success) {
                    document.getElementById('logTime').textContent = resp.last_log || '-';
                }
            });
    }
    loadLogsInfo();

    // 导出日志按钮事件
    document.getElementById('exportLogBtn').onclick = function() {
        window.location.href = '/api/logs/export';
        setTimeout(loadLogsInfo, 1000);
    };

    // 门店基础设置保存逻辑
    (function() {
        var form = document.getElementById('store-base-form');
        if (form) {
            form.onsubmit = function(e) {
                e.preventDefault();
                var name = document.getElementById('store-name').value.trim();
                var time = document.getElementById('store-time').value.trim();
                var phone = document.getElementById('store-phone').value.trim();
                var addr = document.getElementById('store-addr').value.trim();
                if (!name || !time || !phone || !addr) {
                    alert('请填写完整门店信息');
                    return;
                }
                localStorage.setItem('storeInfo', JSON.stringify({ name, time, phone, addr }));
                if (window.updateStoreInfoBar) window.updateStoreInfoBar();
                alert('门店信息保存成功！');
            };
            // 页面加载时自动填充表单
            var info = JSON.parse(localStorage.getItem('storeInfo') || 'null');
            if (info) {
                document.getElementById('store-name').value = info.name || '';
                document.getElementById('store-time').value = info.time || '';
                document.getElementById('store-phone').value = info.phone || '';
                document.getElementById('store-addr').value = info.addr || '';
            }
        }
    })();
    </script>
</body>
</html>